﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAO;

namespace Thefaceless
{
    public partial class FRevenue : Form
    {
        public FRevenue()
        {
            InitializeComponent();
        }

        // Kiểm tra giá trị của combobox Kiểu thống kê
        private String CheckRevenueValue()
        {
            String value = this.cbValueRevenue.Text.ToString();
            if (value.Equals("Theo tháng"))
                return "Month";
            else
                if (value.Equals("Theo loại đồ uống"))
                return "du";
            return null;
        }

        // Chuyển date theo dạng mm/dd/yyyy
        private string ChangeDateFormat(string d)
        {
            return d.Substring(3, 2) + "/" + d.Substring(0, 2) + "/" + d.Substring(6, 4);
        }

        // Bat su kien kiểu thống kê
        private void btnChooseRevenueType_Click(object sender, EventArgs e)
        {
            DataProvider dp = new DataProvider();
            String dayStart = ChangeDateFormat(this.dtDayStart.Value.ToString());
            String dayEnd = ChangeDateFormat(this.dtDayFinish.Value.ToString());
            String sql = "select COUNT(distinct HOADON.MaHD) ,SUM(CHITIETHOADON.Tong),sum(CHITIETHOADON.SoLuong) from HOADON join CHITIETHOADON on HOADON.MaHD = CHITIETHOADON.MaHD where HOADON.NgayHD between '" + dayStart + "' and +'" + dayEnd + "' ";
            DataTable dt = dp.ExecuteQuery(sql);
            this.txtTongHD.Text = dt.Rows[0][0].ToString();
            this.txtTongTien.Text = dt.Rows[0][1].ToString();
            this.txtTongDU.Text = dt.Rows[0][2].ToString();
            if (CheckRevenueValue() != null)
            {
                if (CheckRevenueValue().Equals("Month"))
                {
                    string sqlM = "select MONTH(NgayHD) as 'Tháng',COUNT(MaHD) as 'Tổng hóa đơn',SUM(ThanhTien) as 'Tổng Tiền' from HOADON where HOADON.NgayHD between '" + dayStart + "' and +'" + dayEnd + "' group by MONTH(NgayHD)";
                    DataTable dt1 = dp.ExecuteQuery(sqlM);
                    this.dataRevenue.DataSource = dt1;
                }
                else
                {
                    string sqlDU = "select TenDoUong as 'Tên đồ uống',COUNT(CHITIETHOADON.SoLuong) as 'Tổng số lượng' ,SUM(CHITIETHOADON.Tong) as ' Tổng tiền' from CHITIETHOADON join HOADON on CHITIETHOADON.MaHD = HOADON.MaHD join DANHMUCDOUONG on DANHMUCDOUONG.MaDoUong = CHITIETHOADON.MaDoUong where HOADON.NgayHD between '" + dayStart + "' and +'" + dayEnd + "' group by TenDoUong";
                    DataTable dt1 = dp.ExecuteQuery(sqlDU);
                    this.dataRevenue.DataSource = dt1;
                }
            }
            else
            {
                this.dataRevenue.DataSource = null;
            }
        }




    }
}
